package me.zhengjie.hr.employeeposition.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.hr.employeeposition.domain.EmployeePosition;
import me.zhengjie.hr.employeeposition.service.EmployeePositionService;
import me.zhengjie.hr.employeeposition.service.dto.EmployeePositionQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author ZGZ
* @date 2020-04-15
*/
@Api(tags = "人员职务信息管理")
@RestController
@RequestMapping("/api/employeePosition")
public class EmployeePositionController {

    private final EmployeePositionService employeePositionService;

    public EmployeePositionController(EmployeePositionService employeePositionService) {
        this.employeePositionService = employeePositionService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('employeePosition:list')")
    public void download(HttpServletResponse response, EmployeePositionQueryCriteria criteria) throws IOException {
        employeePositionService.download(employeePositionService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询人员职务信息")
    @ApiOperation("查询人员职务信息")
    @PreAuthorize("@el.check('employeePosition:list')")
    public ResponseEntity<Object> getEmployeePositions(EmployeePositionQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(employeePositionService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增人员职务信息")
    @ApiOperation("新增人员职务信息")
    @PreAuthorize("@el.check('employeePosition:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody EmployeePosition resources){
        return new ResponseEntity<>(employeePositionService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改人员职务信息")
    @ApiOperation("修改人员职务信息")
    @PreAuthorize("@el.check('employeePosition:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody EmployeePosition resources){
        employeePositionService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除人员职务信息")
    @ApiOperation("删除人员职务信息")
    @PreAuthorize("@el.check('employeePosition:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        employeePositionService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}